package org.projetjava.presentation;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.projetjava.metier.model.Lieu;
import org.projetjava.metier.model.Poi;

public class MapScaleEdition extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	BufferedImage image;
    double scale = 1.0;
    public double getScale() {
		return scale;
	}

	public void setScale(double scale) {
		this.scale = scale;
	}

	private Lieu lieu;
    public Lieu getLieu() {
		return lieu;
	}

	public void setLieu(Lieu lieu) {
		this.lieu = lieu;
	}

	private ArrayList<JButton> listboutton = new ArrayList<>();
    private ArrayList<Integer> listX = new ArrayList<>();
    private ArrayList<Integer> listY = new ArrayList<>();
    private ListePoi listePoi;
    
   private void raffraichir()
   {
	   for(Poi poi : lieu.getListPoi())
       {
       	JButton button = new JButton(new ImageIcon("images/gps.png"));
       	
       	button.setOpaque(false);
    	button.setContentAreaFilled(false);
    	button.setBorderPainted(false);
       	
       	button.setToolTipText(poi.getLibelle());
       
       	button.setBounds(poi.getX(), poi.getY(), 40, 40);
       	
       	this.listboutton.add(button);
       	this.listX.add(poi.getX());
       	this.listY.add(poi.getY());

       	this.add(button);
       	
       	//button = null;
       }
   }
   
   public void raffraichirTous()
   {
	   this.removeAll();
	   for(Poi poi : lieu.getListPoi())
       {
       	JButton button = new JButton(new ImageIcon("images/gps.png"));
       	button.setToolTipText(poi.getLibelle());
       
       	button.setBounds(poi.getX(), poi.getY(), 40, 40);
       	button.setOpaque(false);
       	button.setContentAreaFilled(false);
       	button.setBorderPainted(false);
       	
       	this.listboutton.add(button);
       	this.listX.add(poi.getX());
       	this.listY.add(poi.getY());

       	this.add(button);
       	
       	//button = null;
       }
	   repaint();
   }
 
    public MapScaleEdition(BufferedImage image, Lieu lieu, ListePoi listePoi) {
        
    	this.setLayout(null);
    	this.image = image;
        this.setListePoi(listePoi);
        this.lieu = lieu;
        this.listePoi = listePoi;
        
        listePoi.setMap(this);
        
        listePoi.getLabelParcours().setText(lieu.getLibelle());
        
        if(this.lieu.getListPoi().size() > 0)
        {
        	raffraichir();
        }
        
        this.addMouseListener(new MouseAdapter() {
        	
        	public void mouseClicked(MouseEvent e)
        	{
        		
        		JOptionPane jop = new JOptionPane();    	
        	      int option = jop.showConfirmDialog(null, 
        	        "Voulez-vous ajouter un POI ?",
        	        "Ajout POI", 
        	        JOptionPane.YES_NO_CANCEL_OPTION, 
        	        JOptionPane.QUESTION_MESSAGE);

        	      if(option != JOptionPane.NO_OPTION && 
        	      option != JOptionPane.CANCEL_OPTION && 
        	      option != JOptionPane.CLOSED_OPTION){
        	    	  
        	    	 JButton button = new JButton(new ImageIcon("images/gps.png"));
        	    	 
        	    	 button.setOpaque(false);
        	        	button.setContentAreaFilled(false);
        	        	button.setBorderPainted(false);

                  	button.setBounds(e.getPoint().x, e.getPoint().y, 40, 40);
                  	
                  	listboutton.add(button);
                  	listX.add(e.getPoint().x);
                  	listY.add(e.getPoint().y);
                  	


                  	add(button);
                  	Poi poi = new Poi();
                  	poi.setX((int)(e.getPoint().x/getScale()));
                  	poi.setY((int)(e.getPoint().y/getScale()));
                  	
                  	//getLieu().getListPoi().add(poi);
                  	
                  	AjouterPoi ajouterPoi = new AjouterPoi(poi, getListePoi());
                  	
                  	revalidate();
                  	repaint();
        	      }
        		
        		
            	
        	}
        	
		});
    }
 
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        double x = 0;
        double y = 0;
        AffineTransform at = AffineTransform.getTranslateInstance(x,y);
        at.scale(scale, scale);
        g2.drawRenderedImage(image, at);
        
        for(int i = 0 ; i < listboutton.size(); i++)
        {
        	double tempX = listX.get(i)*scale;
        	double tempY = listY.get(i)*scale;
        	
        	double tempX2 = 20*scale;
        	double tempY2 = 35*scale;
        	
        	listboutton.get(i).setBounds((int)tempX-(int)tempX2, (int)tempY-(int)tempY2, (int)(40*scale), (int)(40*scale));
        }
        
    }
 
    public Dimension getPreferredSize() {
        int w = (int)(scale*image.getWidth());
        int h = (int)(scale*image.getHeight());
        return new Dimension(w, h);
    }
 
    public JSlider getSlider() {
        int min = 1, max = 32, inc = 5;
        final JSlider slider = new JSlider(min, max, 16);
        /*slider.setMajorTickSpacing(5);
        slider.setMinorTickSpacing(1);
        slider.setPaintTicks(true);
        slider.setSnapToTicks(true);
        slider.setLabelTable(getLabelTable(min, max, inc));
        slider.setPaintLabels(true);*/
        slider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                int value = slider.getValue();
                scale = (value+4)/20.0;
                revalidate();
                repaint();
            }
        });
        return slider;
    }
 
    private Hashtable getLabelTable(int min, int max, int inc) {
        Hashtable<Integer,JLabel> table = new Hashtable<Integer,JLabel>();
        for(int j = min; j <= max; j += inc) {
            String s = String.format("%.2f", (j+4)/20.0);
            table.put(Integer.valueOf(j), new JLabel(s));
        }
        return table;
    }

	public ListePoi getListePoi() {
		return listePoi;
	}

	public void setListePoi(ListePoi listePoi) {
		this.listePoi = listePoi;
	}

}
